package com.dragee.modules.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.config.exception.AuthException;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.vo.LoginRefereeVo;
import com.dragee.modules.work.dao.CompetitionOrderRefereeDao;
import com.dragee.modules.work.entity.CompetitionOrderRefereeEntity;
import com.dragee.modules.work.service.CompetitionOrderRefereeService;
import com.dragee.modules.work.service.CompetitionOrderService;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("competitionOrderRefereeService")
public class CompetitionOrderRefereeServiceImpl extends ServiceImpl<CompetitionOrderRefereeDao, CompetitionOrderRefereeEntity> implements CompetitionOrderRefereeService {


    @Resource
    private CompetitionOrderRefereeDao competitionOrderRefereeDao;
    @Autowired
    private CompetitionOrderService competitionOrderService;

    private String business = "competitionOrderReferee";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<CompetitionOrderRefereeEntity> wrapper =
                WrapperUtil.getWrapper(business, params, CompetitionOrderRefereeEntity.class);
        IPage<CompetitionOrderRefereeEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }



    @Override
    public LoginUser checkOrderReferee(LoginRefereeVo vo) {

        CompetitionOrderRefereeEntity referee = getOne(new QueryWrapper<CompetitionOrderRefereeEntity>()
                .eq("idCard", vo.getIdCard())
                .eq("name", vo.getName())
                .eq("loginStatus", "1"), false);
        if (referee == null) {
            throw new AuthException("暂时没有权限登录");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(referee.getId());
        loginUser.setUsername(referee.getName());
        loginUser.setType("caiPan");
        return loginUser;
    }

    public static void main(String[] args) {
        String idCard = "370284199410280811";
        System.out.println(Long.parseLong(idCard));
    }


}